<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="manualAcctInfoOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="调账金额（元）"
                       v-bind="formItemLayout">
            <a-input v-decorator="['adjustAmt',{rules: [{ required: true, message: '必填' },
                                  { pattern: '^[0-9]+(.[0-9]{0,2})?$', message: '只能输入正数，包含整数和浮点数， 小数点后只能保留两位'}]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="转出账户"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectPayerAcctInfo"
              @search="selectPayerAcctInfo"
              :disabled="this.isUpd"
              v-decorator="['payerAcctInfo',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in payerAcctInfoData" :key="index">
                {{ d.id }}-{{d.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="转入账户"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectPayeeAcctInfo"
              @search="selectPayeeAcctInfo"
              :disabled="this.isUpd"
              v-decorator="['payeeAcctInfo',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in payeeAcctInfoData" :key="index">
                {{ d.id }}-{{d.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24" v-show="this.title !==  '新增'">
          <a-form-item label="调账状态"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="true"
              v-decorator="['acctStatus',{rules: [{ required: this.isAdd, message: '必填' }]}]">
              <a-select-option key="00">待记账</a-select-option>
              <a-select-option key="01">记账成功</a-select-option>
              <a-select-option key="02">记账失败</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row v-show="this.acctStatusTmp02">
        <a-col :md="12" :sm="24">
          <a-form-item label="失败原因"
                       v-bind="formItemLayout">
            <a-input v-decorator="['respDesc',{rules: [{ required: this.acctStatusTmp02, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row v-show="this.acctStatusTmp01">
        <a-col :md="12" :sm="24">
          <a-form-item label="核心日期"
                       v-bind="formItemLayout">
            <a-input v-decorator="['acctTime',{rules: [{ required: this.acctStatusTmp01, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="核心流水号"
                       v-bind="formItemLayout">
            <a-input v-decorator="['acctSeqNum',{rules: [{ required: this.acctStatusTmp01, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="备注"
                       v-bind="formItemLayout">
            <a-textarea v-decorator="['adjustReason',{rules: [{ required: true, message: '必填' }]}]"
                        :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>

        <a-col :md="12" :sm="24" v-show="this.optFlag == 'chk'">
        <a-form-item label="审批意见"
                     v-bind="formItemLayout">
          <a-textarea v-if="" v-decorator="['auditRefuseReason',{rules: [{ required: true, message: '必填' }]}]"
                      :disabled="this.isUpd"/>
        </a-form-item>
      </a-col>
        <a-col :md="12" :sm="24" v-show="this.optFlag == 'view'">
          <a-form-item label="审批意见"
                       v-bind="formItemLayout">
            <a-textarea v-if="" v-decorator="['auditRefuseReason']"
                        :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.isView == true">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'ManualAcctInfoOperation',
    props: {
      manualAcctInfoOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增',
        isAdd: true,
        isUpd: false,
        isView: true,
        acctStatusTmp01: false,
        acctStatusTmp02: false,
        loading: false,
        optFlag: '',
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        payerAcctInfoData: [],
        payeeAcctInfoData: [],
        info: {}
      }
    },
    computed: {
      show: {
        get: function () {
          return this.manualAcctInfoOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      selectPayerAcctInfo(val) {
        this.payerAcctInfoData = []

        let param = {}
        let payerAcctInfo = 'payerAcctInfo'
        if (val) {
          param = {
            q: encodeURIComponent(val + `&${this.instId}` + `&${payerAcctInfo}`),
            pageSize: 20
          }
        } else {
          param = {
            q: encodeURIComponent(`&${this.instId}` + `&${payerAcctInfo}`),
            pageSize: 20
          }
        }
        this.$get('/admin/csmc/cRoleAcctInfo/cRoleAcctInfoSelect2List', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.payerAcctInfoData.push(r);
          })
        })

      },
      selectPayeeAcctInfo(val) {
        this.payeeAcctInfoData = []

        let param = {}
        let payeeAcctInfo = 'payeeAcctInfo'
        if (val) {
          param = {
            q: encodeURIComponent(val + `&${this.instId}` + `&${payeeAcctInfo}`),
            pageSize: 20
          }
        } else {
          param = {
            q: encodeURIComponent(`&${this.instId}` + `&${payeeAcctInfo}`),
            pageSize: 20
          }
        }
        this.$get('/admin/csmc/cRoleAcctInfo/cRoleAcctInfoSelect2List', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.payeeAcctInfoData.push(r);
          })
        })

      },


      reset() {
        this.loading = false
        this.isStatic = false
        this.isUpd = false
        this.isAdd = false
        this.acctStatusTmp01 = false
        this.acctStatusTmp02 = false
        this.title = '新增'
        this.isView = true
        this.optFlag = ''
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改') {
              this.info.optFlag = 'upd'
            }

            this.$post('/admin/upmp/bManualAcctInfo', {
              ...this.info
            }).then((r) => {
              this.reset()
              this.$emit('success')
            }).catch(() => {
              this.loading = false
            })


          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue( ['id', 'adjustAmt', 'payerAcctInfo', 'payeeAcctInfo', 'acctStatus',
          'respDesc', 'acctTime', 'acctSeqNum', 'adjustReason', 'auditRefuseReason'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setManualAcctInfoFormValues({...info}) {
        let fields = ['id', 'adjustAmt', 'payerAcctInfo', 'payeeAcctInfo', 'acctStatus', 'respDesc', 'acctTime',
          'acctSeqNum', 'adjustReason', 'auditRefuseReason']
        if (info.acctStatus == '01') {
          this.acctStatusTmp01 = true
        }
        if (info.acctStatus == '02') {
          this.acctStatusTmp02 = true
        }

        if (info.operation == 'static') {
          this.isStatic = true
          this.isUpd = true
          this.title = '挂账状态'
        } else if (info.operation == 'view') {
          this.isView = false
          this.isUpd = true
          this.title = '详情'
          this.optFlag = 'view'
        } else if (info.operation == 'edit') {
          this.title = '修改'
        } else {
          this.isAdd = false
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>





